package com.lrsEducation.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lrsEducation.Vo.Registeruser;
import com.lrsEducation.entity.UcenterMember;
import com.lrsEducation.mapper.UcenterMemberMapper;
import com.lrsEducation.service.IUcenterMemberService;
import com.lrsEducation.utils.loginUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Mr.tang
 * @since 2022-05-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements IUcenterMemberService {

    /**
    * 注册用户
    * */
    @Override
    public void saveUser(Registeruser registeruser) {
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile( registeruser.getMobile());
        Map<String, String> password = loginUtil.createMd5(registeruser.getPassword());
        ucenterMember.setPassword(password.get("password"));
        ucenterMember.setInscription(password.get("random"));
        ucenterMember.setIsDeleted(false);
        ucenterMember.setIsDeleted(false);
        baseMapper.insert(ucenterMember);

    }

    /**
     * TODO
     * 用户登录
     * */
    @Override
    public boolean userLogin(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        //判断密码与手机号是否为空
        if (mobile.isEmpty() || password.isEmpty()){
            return false;
        }
        //判断密码是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Long aLong = baseMapper.selectCount(queryWrapper);
        if (aLong == 0 ){
            return false;
        }else {
            UcenterMember user = baseMapper.selectOne(queryWrapper);
            //判断用户是否被禁用
            if (user.getIsDisabled() == true){
                return false;
            }else{
                //判断密码是否正确
                String truepassword = user.getPassword();
                String userpassword = loginUtil.doMd5(ucenterMember.getPassword()+user.getInscription());
                return truepassword.equals(userpassword);
            }
        }
    }

    @Override
    public int getRegisterCount(String date) {
        int count = baseMapper.selectRCount(date);
        return count;
    }
}
